package cn.sun.pet.controller;

import cn.sun.basic.util.AjaxResult;
import cn.sun.basic.util.LoginContext;
import cn.sun.basic.util.PageBean;
import cn.sun.pet.domain.Pet;
import cn.sun.pet.domain.PetDetail;
import cn.sun.pet.query.PetQuery;
import cn.sun.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {


    @Autowired
    private IPetService petService;

    //添加或修改-差异在于是否有id put
    @PutMapping
    @ApiOperation(value = "宠物添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        //通过id判断是添加或者修改
        try {
            if (pet.getId()!=null){
                petService.update(pet);
            }else {
                petService.save(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //Get http://localhost:8080/deparment/1 deparment/2
    public Pet getById(@PathVariable("id") Long id){
        return petService.findById(id);
    }

    //获取所有 get
    @GetMapping ///pet
    public List<Pet> getAll(){
        return petService.findAll();
    }

    //分页+高级查询 //post /pet 传递对象都往@RequestBody -----后台使用的不用区别是否上架了
    @PostMapping
    public PageBean<Pet> list(@RequestBody PetQuery query){
        return petService.findPage(query);
    }


    //前台只查上架了的
    @PostMapping("/list")
    public PageBean<Pet> query(@RequestBody PetQuery query){
        //上架了的---就是一个查询条件
        query.setState(1);
        query.setPageSize(12);
        return petService.findPage(query);
    }


    //获取一个 get
    @GetMapping("/detail/{petId}") //Get http://localhost:8080/deparment/1 deparment/2
    public PetDetail getDetailByPetId(@PathVariable("petId") Long petId){
        return petService.getDetailByPetId(petId);
    }
    @GetMapping("/petDetail/{petId}") //Get http://localhost:8080/deparment/1 deparment/2
    public Pet getPetDetailByPetId(@PathVariable("petId") Long petId){
        return petService.getPetDetailByPetId(petId);
    }

    //上架
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsaleOrOffsale(ids,1); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败！"+e.getMessage());
        }

    }

    //下架
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.onsaleOrOffsale(ids,0); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("下架失败！"+e.getMessage());
        }

    }

    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        Long loginInfoId = LoginContext.getLoginInfo(request).getId();

        try {
            petService.adopt(id,loginInfoId); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("包养失败！"+e.getMessage());
        }
    }
}